//
//  BanKaController.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/7.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "BanKaController.h"
#import "BanKaHeadView.h"
#import "BanKaViewCell.h"
#import "BanKaModel.h"
#import "BannerModel.h"

#import "ShowImageController.h"
#import "BanKaUserNewsController.h"

@interface BanKaController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *BKCollectView;
@property (strong, nonatomic) BanKaHeadView * HeadV;
@property (strong, nonatomic) NSMutableArray * BanKaArr;
@property (strong, nonatomic) NSMutableArray * BannerArr;

@property (strong, nonatomic) MBProgressHUD * hud;


@end

@implementation BanKaController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"办卡有奖";
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    self.BKCollectView.collectionViewLayout = layout;
    self.BKCollectView.delegate = self;
    self.BKCollectView.dataSource = self;
    [self.BKCollectView registerNib:[UINib nibWithNibName:@"BanKaViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    [self.BKCollectView registerNib:[UINib nibWithNibName:@"BanKaHeadView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    [self UpLoadDataOfBanKaController];
    [self UpLoadDataOfBannerView];
}

- (void)UpLoadDataOfBannerView{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:@"3" forKey:@"type"];
    [dic setObject:KUseriPhone forKey:@"phone"];
    
    [MainObjectWay PostWithMethod:@"plug" andAction:@"Server" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"返回的信息 = %@",responseObject);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSMutableArray * arr = responseObject[@"data"][@"list"];
            for (int i = 0 ; i < arr.count; i ++) {
                NSMutableDictionary * dic = arr[i];
                BannerModel * model = [BannerModel mj_objectWithKeyValues:dic];
                [self.BannerArr addObject:model];
                
            }
            [self.HeadV AddBannerImage:self.BannerArr];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"错误信息 = %@",error);
    }];
}

- (void)UpLoadDataOfBanKaController{
    
    self.hud = [MainObjectWay CreateHUDView:self.view];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    
    [MainObjectWay PostWithMethod:@"handle_list" andAction:@"Handle" andAddParam:YES parameters:dic success:^(id responseObject) {
        
        NSLog(@"请求得到的信息 = %@",responseObject);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSMutableArray * arr = responseObject[@"data"];
            for (int i = 0; i < arr.count; i ++) {
                NSMutableDictionary * dic = arr[i];
                BanKaModel * model = [BanKaModel mj_objectWithKeyValues:dic];
                [self.BanKaArr addObject:model];
            }
        }
        
        [self.BKCollectView reloadData];
        [self.hud hideAnimated:YES];
    } failure:^(NSError *error) {
        [self.hud hideAnimated:YES];
        NSLog(@"错误信息 = %@",error);
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.BanKaArr.count;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(8, 8, 8, 8);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat w = (KScreenW - 16)/2;
    return CGSizeMake(w, 96);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    BanKaViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell CreateBanKaViewCell];
    cell.tag = indexPath.row;
    BanKaModel * model = self.BanKaArr[indexPath.row];
    [cell setModel:model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    BanKaModel * model = self.BanKaArr[indexPath.row];
    ShowImageController * ShowC = [[ShowImageController alloc] init];
    ShowC.BKModel = model;
    [self.navigationController pushViewController:ShowC animated:YES];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(KScreenW, 200);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    self.HeadV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
    [self.HeadV CreateOneControllerHead01];
    return self.HeadV;
}

- (NSMutableArray *)BanKaArr {
    if (!_BanKaArr) {
        _BanKaArr = [NSMutableArray array];
    }
    return _BanKaArr;
}

- (NSMutableArray *)BannerArr {
    if (!_BannerArr) {
        _BannerArr = [NSMutableArray array];
    }
    return _BannerArr;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
